<template>
  <div>
    <el-dialog
      title="处理"
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
    >
      <el-form
        :model="dataForm"
        :rules="dataRule"
        ref="dataForm"
        @keyup.enter.native="dataFormSubmit()"
      >
        <!-- <el-form-item label="处理人" :label-width="formLabelWidth" prop="handUserName">
        <el-input v-model.trim="dataForm.handUserName" placeholder="处理人"> </el-input>
      </el-form-item> -->
        <el-form-item
          label="附件"
          :label-width="formLabelWidth"
          prop="appendixUrl"
        >
          <el-upload
            :action="url + '/file/uploadFileInterface'"
            list-type="picture-card"
            :headers="headers"
            :file-list="fileList"
            :on-success="handleSuccess"
            :on-change="handleChange"
          >
            <i slot="default" class="el-icon-plus"></i>
            <div slot="file" slot-scope="{ file }">
              <img
                class="el-upload-list__item-thumbnail"
                :src="file.url"
                alt=""
              />
              <span class="el-upload-list__item-actions">
                <span
                  class="el-upload-list__item-preview"
                  @click="handlePictureCardPreview(file)"
                >
                  <i class="el-icon-zoom-in"></i>
                </span>
                <span
                  class="el-upload-list__item-delete"
                  @click="handleDownloadImg(file)"
                >
                  <i class="el-icon-download"></i>
                </span>
                <span
                  class="el-upload-list__item-delete"
                  @click="handleRemoveImg(file)"
                >
                  <i class="el-icon-delete"></i>
                </span>
              </span>
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item
          label="处理描述"
          :label-width="formLabelWidth"
          prop="handleDesc"
        >
          <el-input v-model.trim="dataForm.handleDesc" type="textarea">
          </el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button size="small" @click="dialogFormVisible = false"
            >取 消</el-button
          >
          <el-button
            size="small"
            type="primary"
            @click="dataFormSubmit()"
            :disabled="isDisabled"
            >确 定</el-button
          >
        </span>
      </template>
    </el-dialog>
    <el-dialog
      :visible.sync="dialogVisibleImg"
      width="50%"
      style="height: 100%"
    >
      <div style="height: 60%">
        <img style="max-width: 100%" :src="dialogImageUrl" alt="" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { v4 as uuidv4 } from "uuid";
import { getToken } from "@/utils/auth";
export default {
  emits: ["refreshDataList"],
  data() {
    return {
      visible: false,
      dialogFormVisible: false,
      isDisabled: false,
      isval: false,
      isupdate: 0,
      istype: "",
      dataForm: {
        id: 0,
        handleUserId: "", //处理人
        handUserName: "", //处理人
        handleDesc: "", //处理描述
        appendixUrl: "", //附件路径
        appendixName: "", //附件名称
      },
      handleState: 0,
      productModelOptions: [],
      fileList: [],
      url: process.env.VUE_APP_BASE_API,
      headers: {
        token: getToken(), //从cookie里获取token，并赋值  Authorization ，而不是token
      },
      dialogVisibleImg: false,
      dialogImageUrl: "",
      dataRule: {
        driverName: [{ required: true, message: "司机姓名必填" }],
        driverPhone: [{ required: true, message: "司机联系电话必填" }],
        licenseNumber: [{ required: true, message: "车牌号必须填写" }],
      },
      formLabelWidth: "70px",
      visible: false,
      // url: process.env.VUE_APP_BASE_API,
    };
  },
  created() {},
  methods: {
    async init(id, handleState) {
      this.dataForm.id = id || 0;
      this.dialogFormVisible = true;
      this.handleState = handleState;
      this.fileList = [];
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.dataForm.productModel = "";
        this.productModelOptions = [];
        console.log(this.$store.state.user);
        this.dataForm.handUserName = this.$store.state.user.name;
        this.dataForm.handleUserId = this.$store.state.user.userId;
        if (!this.handleState) {
          // 新增
          // this.treeListTreeSetCurrentNode()
        } else {
          //获取详情
          this.Sevice({
            url: "/syn/Feedback/info/" + this.dataForm.id,
            method: "get",
          }).then(async (res) => {
            // console.log(res);
            if (res.code != 200) {
              // this.$message.error(res.data.msg)
            } else {
              this.dataForm.id = res.info.id;
              this.dataForm.handleDesc = res.info.handleDesc;
              this.dataForm.handleUserId = res.info.handleUserId;
              this.dataForm.handUserName = res.info.handUserName;
            }
          });
        }
      });
    },
    handleSuccess(response, file, fileList) {
      if (response.code == 200) {
        this.fileList = fileList;
      } else {
        for (let i = 0; i < fileList.length; i++) {
          if (fileList[i].uid == file.uid) {
            fileList.splice(i, 1);
          }
        }
        this.$message({ message: response.msg, type: "error", duration: 1000 });
      }
    },
    handleRemoveImg(file) {
      console.log(file, this.fileList);
      for (let i = 0; i < this.fileList.length; i++) {
        if (this.fileList[i].uid == file.uid) {
          this.fileList.splice(i, 1);
        }
      }
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisibleImg = true;
    },
    handleDownloadImg(file) {
      this.getImge(file);
      this.fileDown(file);
    },
    handleChange(file, fileList) {
      this.fileList = fileList;
    },
    // 表单提交
    async dataFormSubmit() {
      this.$refs["dataForm"].validate(async (valid) => {
        if (valid) {
          //附件
          let appendixUrl = [];
          let appendixName = [];
          console.log(this.fileList);
          if (this.fileList.length) {
            for (let i = 0; i < this.fileList.length; i++) {
              if (this.fileList[i].response) {
                appendixUrl.push(this.fileList[i].response.map.filePath);
              } else {
                appendixUrl.push(this.fileList[i].url);
              }
              appendixName.push(this.fileList[i].name);
            }
          }
          this.dataForm.appendixUrl = appendixUrl.toString();
          this.dataForm.appendixName = appendixName.toString();
          let url = "/syn/Feedback/handle";
          this.isDisabled = true;
          await this.Sevice({
            url: url,
            method: "post",
            data: {
              id: this.dataForm.id || undefined,
              handleDesc: this.dataForm.handleDesc,
              handleUserId: this.dataForm.handleUserId,
              handleState: 1,
              handUserName: this.dataForm.handUserName,
              appendixName: this.dataForm.appendixName,
              appendixUrl: this.dataForm.appendixUrl,
            },
          })
            .then((res) => {
              if (res && res.code == 200) {
                this.dialogFormVisible = false;
                this.$message({
                  message: res.msg,
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.isDisabled = false;
                    this.$emit("getMenuList");
                  },
                });
              } else {
                this.isDisabled = false;
              }
            })
            .catch((error) => {
              console.log(error);
              this.isDisabled = false;
            });
        }
      });
    },
  },
};
</script>

<style scoped>
.mod-menu .menu-list__input > .el-input__inner,
.mod-menu .icon-list__input > .el-input__inner {
  cursor: pointer;
}

.mod-menu__icon-popover {
  width: 458px;
  overflow: hidden;
}

.mod-menu__icon-inner {
  width: 478px;
  max-height: 258px;
  overflow-x: hidden;
  overflow-y: auto;
}

.mod-menu__icon-list {
  width: 458px;
  padding: 0;
  margin: -8px 0 0 -8px;
}

.mod-menu__icon-list > .el-button {
  padding: 8px;
  margin: 8px 0 0 8px;
}

.mod-menu__icon-list > .el-button > span {
  display: inline-block;
  vertical-align: middle;
  width: 18px;
  height: 18px;
  font-size: 18px;
}

.mod-menu .icon-list__tips {
  font-size: 18px;
  text-align: center;
  color: #e6a23c;
  cursor: pointer;
}

.el-tree {
  height: 200px;
  overflow-y: auto;
}
</style>
<style>
.formItem .el-form-item__label {
  float: none;
}
</style>
